package com.turboconnard.display;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.turboconnard.tools.Textures;

public class Plane extends Sprite {
	public static int CC =0;
	public static int TL =1;
	/** The buffer holding the vertices */
	protected FloatBuffer vertexBuffer;
	/** The buffer holding the texture coordinates */
	protected FloatBuffer textureBuffer;
	/** The buffer holding the indices */
	protected ByteBuffer indexBuffer;
	public int texture = -1;
	protected float textureMap[] = {       		
			0.0f, 1.0f,
			1.0f, 1.0f,
			0.0f, 0.0f,
			1.0f, 0.0f,
    };
	protected byte indices[] = {0,1,3,0,3,2};
	private float[] vertices;
	public Plane() {
		super();
		
	}
	/**
	 * 
	 * @param pWidth
	 *            in pixel
	 * @param pHeight
	 *            in pixel
	 */
	public Plane(int pWidth, int pHeight) {
		super();
		width = pWidth*stage.correction;
		height = pHeight*stage.correction;
		init(0);
	}
	public Plane(int pWidth, int pHeight,int pAlign) {
		super();
		width = pWidth*stage.correction;
		height = pHeight*stage.correction;
		init(pAlign);
	}
	public void init(int pAlign){
		switch(pAlign){
			case 0:
				float[] verts0 = {
						-width/2, -height/2, 0, //Vertex 0
			    		width/2, -height/2, 0,  //v1
			    		-width/2, height/2, 0,  //v2
			    		width/2, height/2, 0,   //v3
			   	};
				vertices = verts0;
			break;
			case 1:
				float[] verts1 = {
						0,0,0,
						0,-height,0,
			    		width,0,0,
			    		width,-height,0,
				};
				vertices = verts1;
			break;
		
		}
		
		ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		//
		byteBuf = ByteBuffer.allocateDirect(textureMap.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		textureBuffer = byteBuf.asFloatBuffer();
		textureBuffer.put(textureMap);
		textureBuffer.position(0);

		//
		indexBuffer = ByteBuffer.allocateDirect(indices.length);
		indexBuffer.put(indices);
		indexBuffer.position(0);
	}
	@Override
	protected void _draw(GL10 gl) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glFrontFace(GL10.GL_CCW);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
		int mode;
		if (texture != -1) {
			mode = GL10.GL_TRIANGLES;
			gl.glEnable(GL10.GL_BLEND);
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, Textures.getInstance().getTexture(texture));
			gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
			mode = GL10.GL_TRIANGLES;
		}else{
			mode = GL10.GL_LINE_LOOP;
		}
		gl.glDrawElements(mode, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer);
			
		gl.glDisable(GL10.GL_TEXTURE_2D);
		gl.glDisable(GL10.GL_BLEND);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}
	
}
